package com.example.admin.testdemo.util;

import android.annotation.SuppressLint;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateTimeUtil {

	/**
	 * 获取系统当前日期（格�?012-12-28�?
	 * 
	 * @return
	 */
	public static String getSysDateYMD() {
		return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd");
	}

	/**
	 * 获取系统当前时间（格�?3:58:00�?
	 * 
	 * @return
	 */
	public static String getSysTimeHMS() {
		return getDate(new Date(System.currentTimeMillis()), "hh:mm:ss");
	}

	/**
	 * 获取系统当前时间（格�?012-12-28 13:58:00�?
	 * 
	 * @return
	 */
	public static String getSysTimeYMDHMS() {
		return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss");
	}
	public static String getSysTimeYMDHMSNoFormat() {
		String temp=getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss");
		String t=temp.replace("-","");
		String tt=t.replace(" ","");
		String ttt=tt.replace(":","");
		return ttt;
	}

	public static String getSysTimeYMDHM() {
		return getDate(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm");
	}

	public static String getDate(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 格式时间，如果不是两位数的前面补0
	 * 
	 * @param x
	 * @return
	 */
	public static String format(int x) {
		String s = "" + x;
		if (s.length() == 1) {
			s = "0" + s;
		}
		return s;
	}

	/**
	 * 字符串转日期
	 * 
	 * @param dateStr
	 * @param formatStr
	 * @return
	 */
	public static Date StringToDate(String dateStr, String formatStr) {
		DateFormat dd = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = dd.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	public static String TimestampToString(Timestamp dateStr) {
		if (dateStr != null) {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateStr);
		} else {
			return null;
		}
	}

	public static Timestamp StringToTimestamp(String dateStr) {
		return Timestamp.valueOf(dateStr);
	}

	public static String TimestampToString(Timestamp dateStr, String format) {
		return new SimpleDateFormat(format).format(dateStr);
	}

	public static Timestamp DoubleToTimespan(double time) {
		return longToTimespan((long) time);
	}

	public static Timestamp longToTimespan(long time) {
		return new Timestamp(time);
	}

	public static double TimespanToDouble(Timestamp time) {
		return time.getTime();
	}

	public static String addDay(Date day, int days) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.DATE, days); // 日期�?
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(c.getTime());
	}

	public static Date addMonth(Date day, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.MONTH, month); // 日期�?
		return c.getTime();
	}

	public static Date addYear(Date day, int years) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.YEAR, years);
		return c.getTime();
	}

	public static Date addHour(Date day, int hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.HOUR, hour); // 时间增加
		return c.getTime();
	}

	public static Date addMinute(Date day, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.MINUTE, minute); // 时间增加
		return c.getTime();
	}

	public static Date addSecond(Date day, int second) {
		Calendar c = Calendar.getInstance();
		c.setTime(day); // 设置当前日期
		c.add(Calendar.SECOND, second); // 时间增加
		return c.getTime();
	}

	public static String formatSeconds(long second) {
		String h = "0";
		String d = "0";
		String s = "0";
		long temp = second % 3600;
		if (second > 3600) {
			h = String.valueOf(second / 3600);
			if (Integer.parseInt(h) < 10) {
				h = "0" + h;
			}
			if (temp != 0) {
				if (temp > 60) {
					d = String.valueOf(temp / 60);
					if (Integer.parseInt(d) < 10) {
						d = "0" + d;
					}
					if (temp % 60 != 0) {
						s = String.valueOf(temp % 60);
						if (Integer.parseInt(s) < 10) {
							s = "0" + s;
						}
					}
				} else {
					s = String.valueOf(temp);
					d = "00";
					if (Integer.parseInt(s) < 10) {
						s = "0" + s;
					}
				}
			} else {
				d = "00";
				s = "00";
			}
		} else {
			h = "00";
			d = String.valueOf(second / 60);

			if (Integer.parseInt(d) > 0 && Integer.parseInt(d) < 10) {
				d = "0" + d;
			}

			if (Integer.parseInt(d) % 60 == 0) {

				h = String.valueOf((Integer.parseInt(d) / 60));
				if (Integer.parseInt(h) > 0 && Integer.parseInt(h) < 10) {
					h = "0" + h;
				}
				d = "00";
			}
			s = "00";
			if (second % 60 != 0) {
				s = String.valueOf(second % 60);
				if (Integer.parseInt(s) > 0 && Integer.parseInt(s) < 10) {
					s = "0" + s;
				}
			}
		}

		return h + ":" + d + ":" + s;

	}

	/**
	 * 时间差，返回�?
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static long diffTime(String start, String end) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date dt_start = formatter.parse(start);
			Date dt_end = formatter.parse(end);
			return (dt_end.getTime() - dt_start.getTime()) / 1000;

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * 日期�?
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static long diffDate(String start, String end) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt_start = formatter.parse(start);
			Date dt_end = formatter.parse(end);

			return (dt_end.getTime() / 1000 - dt_start.getTime() / 1000) / (60 * 60 * 24);

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return 0;
	}

	public static long stringToLong(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dtdate = formatter.parse(date);
			return dtdate.getTime();

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * 获取两个日期之间的间隔天�?
	 * 
	 * @return
	 */
	public static int getGapCount(Date startDate, Date endDate) {
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * 日期大小比较
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static boolean after(String beginDate, String endDate) {
		Date b = DateTimeUtil.StringToDate(beginDate, "yyyy-MM-dd");
		Date e = DateTimeUtil.StringToDate(endDate, "yyyy-MM-dd");
		if (b.after(e)) {
			return true;
		}
		return false;
	}

	public static boolean equal(String beginDate, String endDate) {
		Date b = DateTimeUtil.StringToDate(beginDate, "yyyy-MM-dd");
		Date e = DateTimeUtil.StringToDate(endDate, "yyyy-MM-dd");
		if (b.equals(e)) {
			return true;
		}
		return false;
	}

	public static String getBeforeMonthDate(int month){
		Date date=new Date();
		return getDate(addMonth(date,month),"yyyy-MM-dd HH:mm:ss");
	}
}
